﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameWP
{
    public class ManagerTrap
    {
#region Properties
        private List<Texture2D> _listIconInShop;
        public List<Texture2D> ListIconInShop
        {
            get { return _listIconInShop; } 

            set { _listIconInShop = value; }
        }
        private SpriteFont _textFont;
        private List<Trap> _listAllTraps;
        public List<Trap> ListAllTraps
        {
            get { return _listAllTraps; }
            set { _listAllTraps = value; }
        }
        private List<int> _listTrapsHave;
        public List<int> ListTrapsHave
        {
            get { return _listTrapsHave; }
            set { _listTrapsHave = value; }
        }
        private List<Trap> _listTrapActive;
        public List<Trap> ListTrapActive
        {
            get { return _listTrapActive; }
            set { _listTrapActive = value; }
        }

        private List<Button> _listButtonTrapHave;
        public List<Button> ListButtonTrapHave
        {
            get { return _listButtonTrapHave; }
            set { _listButtonTrapHave = value; }
        }

        private Button _up;
        public Button Up
        {
            get { return _up; }
            set { _up = value; }
        }

        private Button _down;
        public Button Down
        {
            get { return _down; }
            set { _down = value; }
        }

        private bool _isHaveTrap;
        private bool _isOpen;
        public bool IsOpen
        {
            get { return _isOpen; }
            set { _isOpen = value; }
        }
        private List<Button> _listCountTrap;
        
#endregion

#region Initilation
        public ManagerTrap(List<Trap> listTraps, List<Texture2D> listIconInShop, Texture2D UpIcon, Texture2D DownIcon, SpriteFont textFont)
        {
            _listAllTraps = listTraps;
            _listIconInShop = listIconInShop;
            _textFont = textFont;

            //Init
            _listTrapsHave = new List<int>();
            for (int i = 0; i < _listIconInShop.Count - 2; i++)
                _listTrapsHave.Add(0);
            _listTrapActive = new List<Trap>();
            _listButtonTrapHave = new List<Button>();
            _listCountTrap = new List<Button>();
            for (int i = 0; i < _listAllTraps.Count; i++)
            {
                _listButtonTrapHave.Add(new Button(_listIconInShop[i + 2], new Vector2(-10000,-10000)));
                _listButtonTrapHave[i].Selected += new Button.IsSelected(TrapHave_Selected);
                _listCountTrap.Add(new Button("x" , textFont, Vector2.Zero, Color.Black) { HaveSound = false});
            }
            _isOpen = false;
            _up = new Button(UpIcon, new Vector2(25, 80));
            _up.Selected += new Button.IsSelected(_up_Selected);
            _down = new Button(DownIcon, new Vector2(25, 80));
            _down.Selected += new Button.IsSelected(_down_Selected);
        }        
#endregion

#region  Method
        private Trap CreateTrap(int index, Vector2 positionSetTrap)
        {
            Trap trap;
            switch (index)
            {
                case 0:
                    return trap = new BombTrap(_listAllTraps[index].ListTextures,
                        positionSetTrap, _listAllTraps[index].Damage,
                        _listAllTraps[index].TimeLive, _listAllTraps[index].Delay,
                        _listAllTraps[index].Price, _listAllTraps[index].ScaleTexture);
                    break;
                case 1:
                    return trap = new SnowTrap(_listAllTraps[index].ListTextures,
                        positionSetTrap, _listAllTraps[index].Damage,
                        _listAllTraps[index].TimeLive, _listAllTraps[index].Delay,
                        _listAllTraps[index].Price, _listAllTraps[index].ScaleTexture);
                    break;
                case 2:
                    return trap = new FireTrap(_listAllTraps[index].ListTextures,
                        positionSetTrap, _listAllTraps[index].Damage,
                        _listAllTraps[index].TimeLive, _listAllTraps[index].Delay,
                        _listAllTraps[index].Price, _listAllTraps[index].ScaleTexture);
                    break;
                default: break;
            }
            return null;
        }

        private void UseTrapHave(int index, Vector2 positionSetTrap)
        {
            if (_listTrapsHave[index] > 0)
            {
                _listTrapsHave[index]--;
                _listTrapActive.Add(CreateTrap(index, positionSetTrap));
            }
        }

        public void AddTrapHave(int index)
        {
            _listTrapsHave[index]++;
        }

        public void TrapHave_Selected(int input)
        {
            Vector2 positionSetTrap = ManagerGame.luffy.Position;
            switch (input)
            {

                case 0: positionSetTrap.Y -= 70; break;
                case 1: positionSetTrap.Y -= 200; break;
                case 2: positionSetTrap.X += 80; break;
            }            
            UseTrapHave(input, positionSetTrap);
        }

        public void ResetTrap()
        {
            _isOpen = false;
            _isHaveTrap = false;
            _listTrapActive.Clear();
            for (int i = 0; i < _listTrapsHave.Count; i++)
                _listTrapsHave[i] = 0;
        }

        private void _down_Selected(int input)
        {
            _isOpen = true;
        }

        private void _up_Selected(int input)
        {
            _isOpen = false;
        }        
#endregion

#region Update & Draw
        public void ManageUpdate(GameTime gametime, Vector2 touchposition)
        {
            int indexY = -1;
            _isHaveTrap = false;
            for (int i = 0; i < _listTrapsHave.Count; i++)
            {
                if (_listTrapsHave[i] > 0)
                {
                    indexY++;
                    _listButtonTrapHave[i].Position = new Vector2(5, 140 + indexY * 82);
                    _listCountTrap[i].Position = new Vector2(85, 140 + indexY * 82);
                    _listCountTrap[i].Text = "x" + _listTrapsHave[i].ToString();
                    _isHaveTrap = true;
                }
                else
                {
                    _listButtonTrapHave[i].PositionX = -10000;
                    _listCountTrap[i].PositionX = -10000;
                }

            }            

            for (int i = 0; i < _listTrapActive.Count; i++)
                _listTrapActive[i].Update(gametime, touchposition);
        }

        public void ManageDrawConst(SpriteBatch spritebatch, GameTime gametime)
        {
            if (_isHaveTrap)
            {
                if (_isOpen)
                {
                    _up.Draw(spritebatch, gametime);
                    for (int i = 0; i < _listButtonTrapHave.Count; i++)
                    {
                        _listButtonTrapHave[i].Draw(spritebatch, gametime);
                        _listCountTrap[i].Draw(spritebatch, gametime);
                    }
                }
                else _down.Draw(spritebatch, gametime);
            }           
        }

        public void ManageDrawTrapWasUsed(SpriteBatch spritebatch, GameTime gametime)
        {
            for (int i = 0; i < _listTrapActive.Count; i++)
                _listTrapActive[i].Draw(spritebatch, gametime);
        }
#endregion
    }
}
